import fs from 'fs';
import git from "isomorphic-git";
import http from "isomorphic-git/http/node";
import {author} from '../config/git-author'

/*
desc:向dir+filePath文件中写入data;
params:
    filePath中需要包含文件名称
    data:写入的数据
 */
async function writeNewFile(dir,filePath,data){
    await fs.promises.writeFile(dir + filePath, data);
}

/*
desc:将文件添加到git
 */
async function addNewFile(dir,gitdir,filepath){
    await git.add({
        fs,
        dir: dir,
        gitdir,
        filepath
    });
}
/*
desc:将文件提交到git
 */
async function commitNewFile(gitdir,dir,filepath){
    return await git.commit({
        fs,
        gitdir,
        dir,
        author,
        message: 'Added the ' + filepath + ' file'
    });
}
/*
desc:将文件推送到git
 */
async function pushNewFile(dir,gitdir){
    return await git.push({
        fs,
        http,
        dir,
        gitdir,
        remote: 'origin',
        ref: 'master',
        onAuth: () => (author),
    });
}
export {writeNewFile}
export {addNewFile}
export {commitNewFile}
export {pushNewFile}